﻿using System;
using System.Collections.Generic;
using System.Text;
using LuaCEGUI.XML;

namespace LuaCEGUI.Controls
{
    static class EventHandler
    {
        public static void Button_Click(object sender, EventArgs e)
        {
            NormalButton button = (NormalButton)sender;
            string name = ((NormalButton)sender).Name;
            int id = ControlManager.GetIDByName(name);
            string methon = ControlManager.GetEventHandler(id);
            if (methon == null)
            {
                methon = "";
            }
            NormalForm f = (NormalForm)button.Parent;


            Parser p = null;
            string WithValue = null;
            try
            {
                p = new Parser("/Program Files/wmssystem/XML/" + f.Name + ".xml");
                WithValue = p.GetControlAttribute(button.Name, "WithValue");
            }
            catch(Exception ex)
            {
                Console.WriteLine("DBG:Error while loading /Program Files/wmssystem/XML/{0}.xml . Message:{1}",f.Name,ex.Message);
            }

            string encoded = null;
            if (WithValue != null)
            {
                string[] tmp = WithValue.Split(',');
                foreach (string s in tmp)
                {
                    string value = ControlManager.GetControlTextByName(s);
                    encoded += Convert.ToBase64String(System.Text.Encoding.GetEncoding("utf-8").GetBytes(value));
                    encoded += "|";
                }
                encoded = encoded.Substring(0, encoded.Length - 1);
            }
            Console.WriteLine("f.name={0}\tmethon={1}\tencode={2}", f.Name, methon, encoded);
            EventHandler_cpp.Button_Click(f.Name,methon,encoded);
        }
    }
}
